package com.loklok.code.confuse.extension;

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.project.Project;
import com.intellij.util.xmlb.annotations.OptionTag;
import com.loklok.code.confuse.converter.FeignInterfaceInfoConverter;
import com.loklok.code.confuse.model.FeignInterfaceInfo;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;

@State(name = "feignInfo.cache", storages = {@Storage(value = "feignInfo-cache.xml")})
public class FeignInterfaceInfoCache implements PersistentStateComponent<FeignInterfaceInfoCache> {

    @OptionTag(converter = FeignInterfaceInfoConverter.class)
    private Map<String, FeignInterfaceInfo> feignInterfaceInfoMap;


    public static FeignInterfaceInfoCache getInstance(Project project) {
        if(project == null){
            return null;
        }
        return project.getService(FeignInterfaceInfoCache.class);
    }

    public void addUpdateCache(Map<String, FeignInterfaceInfo> feignInterfaceInfoMap){
        this.feignInterfaceInfoMap = feignInterfaceInfoMap;
    }

    public Map<String, FeignInterfaceInfo> getFeignInterfaceInfoMap() {
        return feignInterfaceInfoMap;
    }

    @Override
    public @Nullable FeignInterfaceInfoCache getState() {
        return this;
    }

    @Override
    public void loadState(@NotNull FeignInterfaceInfoCache state) {
        if (state.feignInterfaceInfoMap == null) {
            return;
        }
        this.feignInterfaceInfoMap = state.feignInterfaceInfoMap;
    }
}
